/*
 * WidgetMovimientoWidgetMovimientoContainer.cpp
 *
 *  Created on: Jun 18, 2012
 *      Author: gonzalo
 */

#include "WidgetMovimientoContainer.h"

WidgetMovimientoContainer::WidgetMovimientoContainer(int posX, int posY,SDL_Surface* surface, Widget *parent)
:Widget(posX, posY,surface, parent){}


bool WidgetMovimientoContainer::isQueryResult() const
{
    return queryResult;
}

void WidgetMovimientoContainer::add(WidgetMovimiento *widget){
	widgets.push_back(widget);
}

void WidgetMovimientoContainer::showOn(SDL_Surface* surface) {
	std::list<WidgetMovimiento*>::iterator it_widget;
	for (it_widget = widgets.begin(); it_widget != widgets.end(); ++it_widget) {
		if ((*it_widget)->isDrawable() || (*it_widget)->getType() == SHIP)	(*it_widget)->showOn(surface);
	}
}

void WidgetMovimientoContainer::handleEvent(Event event) {
	std::list<WidgetMovimiento*>::iterator it_widget;
	for (it_widget = widgets.begin(); it_widget != widgets.end(); ++it_widget) {
		if ((*it_widget)->isDrawable() || (*it_widget)->getType() == SHIP)	(*it_widget)->handleEvent(event);
	}
}

void WidgetMovimientoContainer::resolve() {
	queryResult = false;

	std::list<WidgetMovimiento*>::iterator it_widget = widgets.begin();
	WidgetMovimiento* processing = NULL;
	while (it_widget != widgets.end()){

		processing = (*it_widget);

		if (elementQuering != NONE && !queryResult){
			queryResult = (processing->getType() == elementQuering);
		}

		//es dibujable?
		if (processing->isDrawable() || processing->getType() == SHIP){
			//dibujar
			processing->resolve();

			//Move to next item
			it_widget++;
		}else{
			//Borra de memoria
			if (processing->getType() != BONUS) delete processing;

			//remover
			it_widget = widgets.erase(it_widget);
		}
	}

	elementQuering = NONE;
}

void WidgetMovimientoContainer::setElementQuering(playElement elementQuering){
		this->elementQuering = elementQuering;
}

WidgetMovimientoContainer::~WidgetMovimientoContainer() {
	while(!widgets.empty()){
		delete widgets.back();
		widgets.pop_back();
	}
}

